package com.ys.service.logistics.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.base.CheckByGrid;
import com.ys.entity.flow.YspModel;
import com.ys.entity.hr.ThrOperateCompanyBankInfo;
import com.ys.entity.logistics.TpAmazonAddress;
import com.ys.entity.logistics.TlDecCustomSub;
import com.ys.entity.purchase.TcgInvoiceOffsetItem;
import com.ys.entity.sys.*;
import com.ys.enume.flow.FlowEnum;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.mapper.logistics.LogisticsMapper;
import com.ys.mapper.logistics.TpAmazonAddressMapper;
import com.ys.mapper.logistics.TlDecCustomSubMapper;
import com.ys.mapper.purchase.TcgInvoiceOffsetItemMapper;
import com.ys.service.flow.YspTaskService;
import com.ys.service.logistics.TpAmazonAddressService;
import com.ys.util.CheckUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.Flow;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 报关单
 */
@Service("tpAmazonAddressService")
public class TpAmazonAddressServiceImpl implements TpAmazonAddressService {
	private static final Logger logger = LoggerFactory.getLogger(TpAmazonAddressServiceImpl.class);

	@Autowired
	private TpAmazonAddressMapper tpAmazonAddressMapper;

	@Autowired
	private YspTaskService yspTaskService;

	@Autowired
	private CheckUtil checkUtil;

	@Autowired
	private LogisticsMapper logisticsMapper;

	@Autowired
	private TcgInvoiceOffsetItemMapper tcgInvoiceOffsetItemMapper;

	@Autowired
	private TlDecCustomSubMapper tlDecCustomSubMapper;


	private final Long GRID = 58L;

	/**
	 * 新增
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R add(Map<String, Object> params, Long menuId) throws Exception{
		logger.info("新增传参:"+params);
		TpAmazonAddress tpAmazonAddress = JSON.parseObject(JSON.toJSONString(params), TpAmazonAddress.class);

		if (tpAmazonAddress.getDestinationFulfillmentCenterId().length()>10){
			return R.error("仓库标识字符不能超过10！");
		}
		if (tpAmazonAddress.getName().length()>10){
			return R.error("仓库名称字符不能超过10！");
		}
		if (tpAmazonAddress.getPostalCode().length()>20){
			return R.error("邮编字符不能超过20！");
		}
		if (tpAmazonAddress.getDistrictOrCounty().length()>20){
			return R.error("县或市字符不能超过20！");
		}
		if (tpAmazonAddress.getStateOrProvinceCode().length()>20){
			return R.error("省份字符不能超过20！");
		}
		if (tpAmazonAddress.getCity().length()>20){
			return R.error("城市字符不能超过20！");
		}
		if (tpAmazonAddress.getAddressLine1().length()>50){
			return R.error("仓库地址1字符不能超过50！");
		}
		if (tpAmazonAddress.getAddressLine2().length()>50){
			return R.error("仓库地址2字符不能超过50！");
		}
		TpAmazonAddress entity = JSON.parseObject(JSON.toJSONString(params), TpAmazonAddress.class);
		entity.setStatus(1);//状态



		//新增
		tpAmazonAddressMapper.insert(entity);

		//return R.ok(EditDto.closeAndReload());
		return R.ok(AddDto.gotoEdit("/web/logistics/tpAmazonAddress/toEdit?id=" + entity.getId()));
	}


	/**
	 * 修改
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R update(Map<String, Object> params, Long menuId) throws Exception{
		logger.info("修改传参:"+params);
		TpAmazonAddress entity = JSON.parseObject(JSON.toJSONString(params), TpAmazonAddress.class);


		tpAmazonAddressMapper.update(entity);
		return R.ok(AddDto.gotoEdit("/web/logistics/tpAmazonAddress/toEdit?id=" + entity.getId()));
	}






	//删除
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public R del(Long id) throws Exception{
		if (id == null) {
			return R.error("ID不能为空");
		}
		TpAmazonAddress entity = new TpAmazonAddress().setId(id).queryFirst();
//		if (entity.getEffectStock() == FlowEnum.EffectStock.EFFECTED.value) {
//			return R.error("已生效数据无法删除");
//		}
		// 验证是否制单人
		String loginUserId = ShiroUtils.getUserId();
//		TsUserChange tsUserChange = new TsUserChange().setId(entity.getCreateChangeId()).queryFirst();
//		if (!tsUserChange.getUserId().equals(loginUserId) && !"admin".equals(loginUserId)) {
//			return R.error("非制单人，无法删除");
//		}
		//删除
		tpAmazonAddressMapper.deleteTrue(id);
		//删除列表信息
		logisticsMapper.deleteTpAmazonAddressItemByBid(entity.getId().toString());
		//删除流程
		yspTaskService.flowDelete(entity.getId().toString(), loginUserId);
		return R.ok(EditDto.closeAndReload());
		//return R.ok();
	}



}
